package com.gyx.lottery.common.interceptor;

import com.gyx.lottery.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String user_token = request.getHeader("user_token");
        log.info("获取token为:{}",user_token);
        log.info("解析token路径为:{}",request.getRequestURI());
        Claims claims = JWTUtil.parseJWT(user_token);
        if(claims==null){
            log.info("JWT令牌解析失败，不放行");
            response.setStatus(401);
            return false;
        }
      return true;
    }
}
